package com.lzqinga.controller;

import com.lzqinga.model.ApiResponse;
import com.lzqinga.service.VideoGeneratorService;
import com.lzqinga.service.XxunFeiAIService;
import com.lzqinga.service.storage.LocalStorageService;
import com.lzqinga.service.storage.VideoSegmentService;
import com.lzqinga.util.FileUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * - @author lzqinga
 * - @description
 * - @date 2025/10/10/010 11:59
 **/
@RestController
@RequestMapping("/test")
@RequiredArgsConstructor
@Slf4j
public class TestController {

    @Autowired
    private LocalStorageService storageService;

    private final VideoGeneratorService videoGeneratorService;

    private final VideoSegmentService videoSegmentService;

    private final XxunFeiAIService xxunFeiAIService;

    @RequestMapping("/v1")
    public void test() throws Exception {

        //测试视频合成
//        VideoGeneratorService.Segment segment = new VideoGeneratorService.Segment();
//        segment.audioPath = FileUtil.getDataFilePath("videossegment_1.mp3","data");
//        segment.imagePath = FileUtil.getDataFilePath("videossegment_1.jpg","data");
//        System.out.println("segment = " + segment.audioPath+"############"+segment.imagePath);
//        segment.content = "当冬日的第一片雪花飘落在故宫的红墙之上，这座古老的宫殿便披上了一层银装，仿佛穿越回了千年之前。";
//        videoSegmentService.createVideoSegment(segment);

        //测试超拟人语音合成
        xxunFeiAIService.textToSpeech("当冬日的第一片雪花飘落在故宫的红墙之上，这座古老的宫殿便披上了一层银装，仿佛穿越回了千年之前。",
                "data\\videos",
                "test_audio",null);
    }


    @RequestMapping("/init")
    public ApiResponse<String> init() throws Exception {
        try {
            storageService.initStorageDirs();
            return ApiResponse.success("应用初始化成功");
        } catch (Exception e) {
            log.error("应用初始化失败", e);
            return ApiResponse.error("应用初始化失败: " + e.getMessage());
        }
    }

    @RequestMapping("/generator")
    public ApiResponse<String> generator() throws Exception {
        String finalVideo = "";
        try {
            String textToken = "bce-v3/ALTAK-7E9eb6lRRPwixxdqYc76p/786cadc5603df42c8459dc24aea8df3d175b6548";
            String appId = "app-Rkqmn9vp";
            String imageToken = "bce-v3/ALTAK-7E9eb6lRRPwixxdqYc76p/786cadc5603df42c8459dc24aea8df3d175b6548";
            String ttsToken = "bce-v3/ALTAK-7E9eb6lRRPwixxdqYc76p/786cadc5603df42c8459dc24aea8df3d175b6548";
            String videoToken = "bce-v3/ALTAK-7E9eb6lRRPwixxdqYc76p/786cadc5603df42c8459dc24aea8df3d175b6548";
            String outputDir = "data\\videos";

            finalVideo = videoGeneratorService.generateVideoFromText(
                    "故宫的雪",
                    textToken, appId,
                    imageToken,
                    ttsToken,
                    videoToken,
                    outputDir
            );

            System.out.println("视频生成完成: " + finalVideo);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return ApiResponse.success(finalVideo);
    }
}
